
#include <BFile>
#include <BChangeEvent>
#include <BApplication>
#include "IGlobal.h"
#include "member_BWidget.h"

using namespace BWE;

BApplication::BApplication(int argc, char** argv)
{
	app_done = false;
	app_style = new BStyle(BColor(200, 200, 200));

	const char* libName = "BWE.dll";

#ifdef _WINDOWS_SYSTEM_

#ifdef _DEBUG_
	libName = "BWEd.dll";
#endif
	
	HMODULE module = GetModuleHandle(libName);
	char path[256];
	DWORD len = GetModuleFileNameA(module, path, 256);
	if (len == 0)
	{
		puts("Locate librrary path failed.");
		return;
	}
	app_libPath = path;
	app_libPath.replace('\\', '/');
#endif

	app_libPath -= libName;
}
BApplication::~BApplication()
{

}

void BApplication::setStyleSheet(const BString& styleSheet)
{
	if (BFile::Exists(styleSheet))
	{
		BFile file(styleSheet);
		if (file.open(BFile::Mode_Read))
		{
			file.readString(app_styleSheet);
		}
	}
	else
	{
		app_styleSheet = styleSheet;
	}

	if (!app_styleDriver)
		app_styleDriver = new IStyleDriver();

	if (app_styleSheet.empty())
	{
		app_styleDriver->clear();
		for (int i = 0; i < app_windows.size(); i++)
		{
			IWindow* window = app_windows[i];
			if (window->boss)
			{
				app_styleDriver->apply(window->boss);
			}
		}
	}
	else
	{
		app_styleDriver->init(app_styleSheet);
		for (int i = 0; i < app_windows.size(); i++)
		{
			IWindow* window = app_windows[i];
			if (window->boss)
			{
				app_styleDriver->apply(window->boss);
			}
		}
	}
}
const BString& BApplication::styleSheet()
{
	return app_styleSheet;
}

void BApplication::setStyle(BStyle* style)
{
	if (app_style != style)
	{
		app_style = style;
	}
	for (int i = 0; i < app_windows.size(); i++)
	{
		IWindow* window = app_windows[i];
		if (window->boss)
		{
			widget_member(window->boss)->freshStyleTree();
		}
	}
}
BStyle* BApplication::style()
{
	return app_style;
}

void BApplication::setFont(const BString& family, int size)
{
	BFont* font = new BFont(family, size);
	setFont(font);
}
void BApplication::setFont(BFont* font)
{
	if (app_style)
	{
		app_style->setFont(font);
		setStyle(app_style);
	}
}
const BFont* BApplication::font()
{
	if (app_style)
	{
		return app_style->font();
	}
	return 0;
}

void BApplication::setFrequency(int frequency)
{
	if (app_frequency <= 0)
		app_frequency = 30;
	else
		app_frequency = frequency;
}
int BApplication::frequency()
{
	return app_frequency;
}
int BApplication::realFrequency()
{
	return app_realFrequency;
}

void BApplication::done(int code)
{
	app_code = code;
	app_done = true;
}
int BApplication::code()
{
	return app_code;
}

int BApplication::execute()
{
	while (!app_done)
	{
		App_CycleBegin();
		App_Event();
		App_Update();
		App_Frame();
		App_CycleEnd();
	}
	app_windows.clear();
	app_sockets.clear();
	return app_code;
}

void BApplication::addPluginPath(const BString& pluginPath)
{
	if (!app_pluginPaths.contain(pluginPath))
	{
		app_pluginPaths.append(pluginPath);
	}
}
void BApplication::addFontPath(const BString& fontPath)
{
	if (!app_fontPaths.contain(fontPath))
	{
		app_fontPaths.append(fontPath);
	}
}
void BApplication::addDataPath(const BString& dataPath)
{
	if (!app_dataPaths.contain(dataPath))
	{
		app_dataPaths.append(dataPath);
	}
}

void BApplication::removePluginPath(const BString& pluginPath)
{
	app_pluginPaths.remove(pluginPath);
}
void BApplication::removeFontPath(const BString& fontPath)
{
	app_fontPaths.remove(fontPath);
}
void BApplication::removeDataPath(const BString& dataPath)
{
	app_dataPaths.remove(dataPath);
}

int BApplication::pluginPathCount()
{
	return app_pluginPaths.size();
}
int BApplication::fontPathCount()
{
	return app_fontPaths.size();
}
int BApplication::dataPathCount()
{
	return app_dataPaths.size();
}

const BString& BApplication::pluginPath(int index)
{
	return app_pluginPaths(index);
}
const BString& BApplication::fontPath(int index)
{
	return app_fontPaths(index);
}
const BString& BApplication::dataPath(int index)
{
	return app_dataPaths(index);
}
